package com.evenmo.gxlcTest;


import com.evenmo.pojo.manytoome_danxiang.Person;
import com.evenmo.pojo.manytoome_danxiang.Room;
import com.evenmo.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* @author: moyongjun
* @Date: 2019-08-16 18:36
* @Description: 多对一关联测试
*
*/
public class PersonTest2 {

    private Session session;
    private Transaction transaction;


    @Before
    public void getSession() {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();
    }

    @Test
    public void addPersonTest() {

        Person person= new Person();

        person.setName("test1");
        Room room =new Room();
        room.setAddress("杭州西湖区");
        person.setRoom(room);

        session.save(person);

    }
    @Test
    public void queryPerson(){

        Person person = session.get(Person.class, 3);
        System.out.println(person);

    }

    @Test
    public void queryRoom(){

        Room room = session.get(Room.class, 3);
        System.out.println(room);

    }

    @After
    public void closeSession() {
        try {
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        } finally {
            session.close();
        }

    }
}
